package com.servlet.user;

import com.service.user.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.Collection;

/**
 * 创建作者 ：MySunshine
 * 创建时间 2022/10/25 15:24
 * 项目名称 SHUSHAN
 */
@WebServlet("/UserGetImg")
@MultipartConfig
public class UserAvatar extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       // super.doPost(req,resp);
        System.out.println("表单上传图片");
        //测试获取表单控件的数据
        req.setCharacterEncoding("utf-8");//服务端接收前台数据防止中文乱码
        String num = req.getParameter("num");
        //测试图片上传
        UserServiceImpl usi = new UserServiceImpl();
        boolean b=false;
        Part part = req.getPart("photo");
        if (part==null){
             String file="1.jpg";
             b = usi.UserGetImgByNum(Integer.parseInt(num),file);

        }else {
            String fileName = part.getSubmittedFileName();
            String path = "C:\\Users\\MySunshine\\Desktop\\冯德祥-书山立志小说网\\SHUSHAN IS DETERMINED\\SHUSHAN\\web\\userimg\\"+fileName;
            part.write(path);
            System.out.println(path);
            System.out.println("得到图片");
            b = usi.UserGetImgByNum(Integer.parseInt(num),fileName);
        }
        if (b){
            System.out.println("上传成功！");
            resp.sendRedirect("/page/userLogin.html");
        }else {
            System.out.println("上传失败！");
            resp.sendRedirect("/page/logopage.html");
        }
    }
}
